#include "calculator.h"
#include <QMessageBox>

/*
void calculator::resizeWindow(){
  gWin->setGeometry(0,0,96*zoomAmount-1,64*zoomAmount-1);
}
*/

void calculator::paintEvent(QPaintEvent*){
  QPainter painter(this);
  if(drawSkin)painter.drawImage(0,0,*skin);
}

void calculator::adjustKey(float amount,int group,int bit){
  QImage keymap("keymap.bmp");

  int x,y;
  QColor color;
  // Optimize later...I don't need to do ">>4" every time, make Qt_getKeyLocation automatically adjust value
  for(y=246;y<630;y++){
    for(x=31;x<280;x++){
      color.setRgb(keymap.pixel(x,y));
      if(color.green()>>4==group && color.blue()>>4==bit)goto foundKey;
    }
  }
  // If it's not found, it must be somebody clicking not on a key...return
  return;
  foundKey:;
  QColor newColor;
  while(color.green()>>4==group && color.blue()>>4==bit){
    color.setRgb(keymap.pixel(x,y));
    newColor.setRgb(skin->pixel(x,y));
    //newColor.setAlpha(150);
    newColor.setRgb(newColor.red()*amount,newColor.green()*amount,newColor.blue()*amount);
    skin->setPixel(x,y,newColor.rgb());
    x++;
  }
  y++;
  for(x=0;x<280;x++){
    color.setRgb(keymap.pixel(x,y));
    if(color.green()>>4==group && color.blue()>>4==bit)goto foundKey;
  }
  calculator::update();
}

void calculator::keyPressEvent(QKeyEvent* event){
/*
  if(event->key()==0x3b){
  QMessageBox messagebox;
  messagebox.setText("hello");
  messagebox.exec();
}
*/
  if(pkeys.contains(event->key()))return;
  if(macroRunning){mkeys.append(event->key());};
  pkeys.append(event->key());
  keyprog_t* key;
  //printf("key:%u",event->nativeScanCode());
  if(event->nativeScanCode()==50 || event->nativeScanCode()==62){
    key = Qt_getKeyLocation(event->nativeScanCode()*10);
   // printf("SHIFT");
  }
  else{
    key = Qt_getKeyLocation(event->key());
  }
  if(key){
    calculator::adjustKey(.5,key->group,key->bit);
    calc_key_press(calc, event->key());
  }

}

void calculator::keyReleaseEvent(QKeyEvent* event){
  if(!pkeys.contains(event->key()))return;
  pkeys.removeAt(pkeys.indexOf(event->key()));
 keyprog_t* key;
  if(event->nativeScanCode()==50 || event->nativeScanCode()==62){
    key = Qt_getKeyLocation(event->nativeScanCode()*10);
  }
  else{
    key = Qt_getKeyLocation(event->key());
  }
  if(key){
    calculator::adjustKey(2,key->group,key->bit);
    calc_key_release(calc, event->key());
  }
}

void calculator::mousePressEvent(QMouseEvent* event){
  // 2nd key, width,height= 42,23
  // location = 34,302
  // vertical distance between 2nd-like keys is 37
  // horizontal distance between 2nd-like keys is 49

  // check 2nd-like keys first
  // WabbitEmu uses a weird keycode map thing, TODO: account for exceptions

  // 102,380 in global coordqs should by 4,6

  QImage keymap("keymap.bmp");
  
  QColor color(keymap.pixel( event->x(), event->y() ));
  if(color.red() == 0xFF)return;
  int group=color.green()>>4;
  int bit=color.blue()>>4;
  if ( group==0x05 && bit==0x00){
    //calcs[gslot].cpu.pio.keypad->on_pressed |= KEY_MOUSEPRESS;
    mouseKey=0x01000035;
    calc_key_press(calc,0x01000035);
  }
  else{
    //kp->keys[color.green() >> 4][color.blue() >> 4] |= KEY_MOUSEPRESS;
    /*
      if ((kp->keys[color.green() >> 4][color.blue() >> 4] & KEY_STATEDOWN) == 0) {
    //DrawButtonState(hdcSkin, hdcKeymap, &pt, DBS_DOWN | DBS_PRESS);
    kp->keys[color.green() >> 4][color.blue() >> 4] |= KEY_STATEDOWN;
    //SendMessage(hwnd, WM_SIZE, 0, 0);
    */
/*
    char txt[]={0,0,0,0,0,0,0,0,0,0,0,0,0};
  sprintf(txt,"%ld,%d,%d|%d,%d",Qt_getKeycode(color.green() >> 4,color.blue() >> 4),color.green() >> 4,color.blue() >> 4,event->x(),event->y());
  QString txt2(txt);

  QMessageBox messagebox;
  messagebox.setText(txt2);
  messagebox.exec();
*/
  int keycode = Qt_getKeycode(group,bit);
  if(keycode>0){
    /*if(!pkeys.isEmpty())*/if(pkeys.contains(keycode) || mouseKey==keycode)return;
    mouseKey=keycode;
    calculator::adjustKey(.5,group,bit);
    calc_key_press(calc,keycode);
   // }

  }
  }



/*
  double lx1 = (event->x() - 34)/49;
  double lx = 5-lx1;
  lx+=(int)(lx - ((int)lx) != 0); 

  double ly1 = (event->y() - 302)/37;
  double ly = 7-lx1;
  ly+=(int)(ly - ((int)ly) != 0);

  char txt[5]={0,0,0,0,0};
  sprintf(txt,"%d,%d",(int)lx,(int)ly);
  QString txt2(txt);

  QMessageBox messagebox;
  messagebox.setText(txt2);
  messagebox.exec();
  
  short counter2;
  for(counter2=0;counter2<66;counter2++){
    if(keygrps2[counter2].group==(int)lx && keygrps2[counter2].bit==(int)ly){
      QMessageBox messagebox;
      messagebox.setText("hello");
      messagebox.exec();
      calc_key_press(calc, keygrps2[counter2].vk);
    }
  }
*/
}

void calculator::mouseReleaseEvent(QMouseEvent* event){
  /*
  QImage keymap("keymap.bmp");
  QColor color(keymap.pixel( event->x(), event->y() ));
  if(color.red() == 0xFF)return;
  int group=color.green()>>4;
  int bit=color.blue()>>4;
  if (group==0x05 && bit==0x00){
    //calcs[gslot].cpu.pio.keypad->on_pressed |= KEY_MOUSEPRESS;
    calc_key_release(calc,0x01000035);
  }
  else{
*/
  
    if(!mouseKey)return;
if(mouseKey==0x01000035)calc_key_release(calc,0x01000035);
else{

    keyprog_t* key = Qt_getKeyLocation(mouseKey);
calculator::adjustKey(2,key->group,key->bit);
    calc_key_release(calc,Qt_getKeycode(key->group,key->bit));
}
    mouseKey=0;
    
    //}

 // }
}

void calculator::skinless(){
  if(drawSkin){
    drawSkin=0;
    gWin->setGeometry(0,0,96*zoomAmount-1,64*zoomAmount-1);
  }
  else{
    drawSkin=1;
    gWin->setGeometry(0,0,315,689);
  }
}

void calculator::cutOut(){
  gWin->setWindowFlags(gWin->windowFlags());
}

void calculator::zoom1(){
  zoomAmount=1;
  if(!drawSkin)gWin->setGeometry(0,0,96*zoomAmount-1,64*zoomAmount-1);
}

void calculator::zoom15(){
  zoomAmount=1.5;
  if(!drawSkin)gWin->setGeometry(0,0,96*zoomAmount-1,64*zoomAmount-1);
}

void calculator::zoom2(){
  zoomAmount=2;
  if(!drawSkin)gWin->setGeometry(0,0,96*zoomAmount-1,64*zoomAmount-1);
}

void calculator::zoom3(){
  zoomAmount=3;
  if(!drawSkin)gWin->setGeometry(0,0,96*zoomAmount-1,64*zoomAmount-1);
}